from myPsychology.odmantic import async_myMongo as psycho_mongo
from myPsychology.odmantic import myQuery as psycho_query
from mydsm5.odmantic import myMongo as dsm_mongo
from mydsm5.odmantic import myQuery as dsm_query

from myProfile.db_config.odmantic_config import create_db_engine
from myProfile.repository.login import LoginRepository
from datetime import datetime
from myProfile.security.secure import get_password_hash

import asyncio
import typer

app = typer.Typer()


@app.command()
def hello1():
    print('Hello, myPsychology!')
    loop = asyncio.get_event_loop()
    loop.run_until_complete(psycho_mongo.main())
    print('after initialization:')
    scales = asyncio.run(psycho_query.main())
    all_scales = []
    categories = []
    for scale in scales:
        all_scales.append(scale.abbreviation)
        categories.append(scale.category)

    categories = sorted(list(set(categories)))
    print(f'length of all scales = {len(all_scales)}')
    print(f'abbr of all acales = {all_scales}')
    print(f'categories = {categories}')


@app.command()
def hello2():
    print('Hello, myDSM!')
    loop = asyncio.get_event_loop()
    loop.run_until_complete(dsm_mongo.main())
    print('after initialization:')
    all_disorders = asyncio.run(dsm_query.main())
    for num, disorder in enumerate(all_disorders):
        print(f'{num+1}.{disorder.chinese_name}:\
              {[tt.chinese_name for tt in disorder.treatment]}')


@app.command()
def hello3():
    print('Hello, myProfile!')
    engine = create_db_engine()
    id = 28485690
    username = 'openminds'
    password = 'openminds2024'
    passphrase = get_password_hash(password)
    permission_sets = ['superior', 'admin_read', 'admin_write', 'user']
    details = {'id': id, 'username': username, 'password': password,
               'passphrase': passphrase, 'approved_date': datetime.today(),
               'permission_sets': permission_sets}
    loginrepo = LoginRepository(engine)
    loop = asyncio.get_event_loop()
    user = loop.run_until_complete(loginrepo.get_login_username(username))
    if user is not None:
        print(f'username {username} already exists!')
        success = False
    else:
        success = loop.run_until_complete(loginrepo.insert_login(details))

    if success:
        print('Add admin successfully!')
    else:
        print('Add admin unsuccessfully!')


if __name__ == "__main__":
    app()
